package com.alipay.bean;

import com.alipay.config.AlipayConfig;
import com.alipay.util.AlipaySubmit;
import com.alipay.util.UtilDate;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cqd on 2016-01-07.
 * 退款请求bean封装
 */
public class Refund {
    //  public String service = "refund_fastpay_by_platform_nopwd";//退款无密接口名称
    public String service = "refund_fastpay_by_platform_pwd";//退款有密接口名称
    public String partner = AlipayConfig.partner;//合作者身份ID
    public String _input_charset = "utf-8";//参数编码字符集
    public String sign_type = AlipayConfig.sign_type;//签名方式
    public String seller_email = AlipayConfig.paymentAccount;//卖家支付宝账号
    public String seller_user_id = AlipayConfig.partner;//卖家用户ID
    public String notify_url = AlipayConfig.refund_notify_url;//通知回调页面
    public String refund_date = UtilDate.getDateFormatter();//退款请求时间
    public String batch_no = UtilDate.getDate() + System.currentTimeMillis();//退款批次号

    public String batch_num;//总笔数
    public String detail_data;//单笔数据集合
    public String sign;//签名

    public String getService() {
        return service;
    }

    public String getPartner() {
        return partner;
    }

    public String get_input_charset() {
        return _input_charset;
    }

    public String getSign_type() {
        return sign_type;
    }

    public String getSeller_email() {
        return seller_email;
    }

    public String getSeller_user_id() {
        return seller_user_id;
    }

    public String getNotify_url() {
        return notify_url;
    }


    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public void setNotify_url(String notify_url) {
        this.notify_url = notify_url;
    }

    public String getRefund_date() {
        return refund_date;
    }


    public String getBatch_no() {
        return batch_no;
    }


    public String getBatch_num() {
        return batch_num;
    }


    public String getDetail_data() {
        return detail_data;
    }

    /**
     * 生成单笔数据集内容和总笔数
     *
     * @param list 退款数据集合
     * @return 支付宝批量申请界面url
     */
    public String builderDetailData(List<RefundDetailData> list) {
        if (list == null || list.size() == 0) {
            return null;
        }
        StringBuffer temp = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            RefundDetailData data = list.get(i);
            String trade_no = data.getAplipay_trade_no();
            String price = data.getPrice();
            String reason = data.getReason();
            temp.append(trade_no);
            temp.append("^");
            temp.append(price);
            temp.append("^");
            temp.append(reason);
            if (i != list.size() - 1) {//不是最后一个，需要添加#号区别开
                temp.append("#");
            }
        }
        this.detail_data = temp.toString();
        this.batch_num = String.valueOf(list.size());
        this.sign = AlipaySubmit.buildNoQuataRequest(toMap());
        return AlipayConfig.refund_url + this.sign;
    }

    //获取类各个字段数值，并生成map,主要用于签名
    public Map<String, String> toMap() {
        Map<String, String> map = new HashMap<String, String>();
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            Object obj;
            try {
                obj = field.get(this);
                if (obj != null) {
                    if (!field.getName().equals("sign_type")) {//过滤掉加密类型
                        //           System.out.println(field.getName() + ":" + obj.toString());
                        map.put(field.getName(), obj.toString());
                    }
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public List<RefundDetailData> getTestData() {
        List<RefundDetailData> list = new ArrayList<RefundDetailData>();
        for (int i = 0; i < 1; i++) {
            RefundDetailData detailData = new RefundDetailData();
            detailData.setAplipay_trade_no("2016011121001004630092154985");
            detailData.setPrice("0.01");
            detailData.setReason("付错了" + i);
            list.add(detailData);
        }
        return list;

    }

    public static void main(String[] args) {
        Refund refund = new Refund();
        refund.builderDetailData(refund.getTestData());
        System.out.println(AlipayConfig.refund_url + refund.sign);
        //  System.out.println(refund.sign_type);
    }
}
